package com.lagou.edu.controller;

import com.lagou.edu.client.AuthCodeClient;
import com.lagou.edu.client.TokenClient;
import com.lagou.edu.entity.User;
import com.lagou.edu.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.net.http.HttpResponse;
import java.util.Date;
import java.util.Optional;

/**
 * @Author: yeqingyun
 * @Date: 2020/11/20 15:42
 */
@RequestMapping("/user")
@RestController
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;
    private final AuthCodeClient authCodeClient;
    private final TokenClient tokenClient;

    @RequestMapping("/register/{email}/{password}/{code}")
    public Integer register(@PathVariable("email") String email, @PathVariable("password") String password, @PathVariable("code") String code, HttpServletResponse httpServletResponse) {
        //1.验证邮箱验证码
        int validRes = authCodeClient.validate(email, code);
        if (validRes > 0) {
            return validRes;
        }
        //2.创建用户
        User user = new User();
        user.setEmail(email);
        user.setPassword(password);
        user.setCreateTime(new Date());
        userService.add(user);
        //3.创建token
        String token = tokenClient.create(email);
        Cookie cookie = new Cookie("token",token);
        cookie.setMaxAge(3600);//最大有效时间
        cookie.setPath("/");
        httpServletResponse.addCookie(cookie);
        return 0;
    }

    @RequestMapping("/isRegistered/{email}")
    public boolean isRegistered(@PathVariable String email) {
        return userService.getByEmail(email).isPresent();
    }

    @RequestMapping("/login/{email}/{password}")
    public String login(@PathVariable("email") String email, @PathVariable("password") String password, HttpServletResponse httpServletResponse) {
        Optional<User> user = userService.getByEmail(email);
        if (!user.isPresent()) {
            return "false";
        }
        if (!password.equals(user.get().getPassword())) {
            return "false";
        }
        //1 创建token
        String token = tokenClient.create(email);
        //2 写入cookie
        Cookie cookie = new Cookie("token",token);
        cookie.setMaxAge(3600);//最大有效时间
        cookie.setPath("/");
        httpServletResponse.addCookie(cookie);
        //2返回邮箱
        return email;
    }

    @RequestMapping("/info/{token}")
    public String token(@PathVariable("token") String token, HttpServletResponse response) {
        String email = tokenClient.email(token);
        if(StringUtils.isEmpty(email)) {
            response.setStatus(HttpStatus.UNAUTHORIZED.value());
        }
        return email;
    }
}
